<template>
  <div class="layout layout-content-box">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="float:left;">新建文章</span>
        <el-button style="float: right;padding: 3px 0" type="text">操作按钮</el-button>
      </div>
      <div class="item-content-box">
        <el-form :model="articleForm" :rules="rules" ref="articleForm" label-width="120px" size="small" class="demo-articleForm">
          <el-form-item label="文章标题" required prop="title">
            <el-input v-model="articleForm.title" placeholder="请输入文章标题"></el-input>
          </el-form-item>
          <el-form-item label="文章简介" required prop="description">
            <el-input type="textarea" v-model="articleForm.description"></el-input>
          </el-form-item>
          <el-form-item label="作者" required>
            <el-input v-model="articleForm.author" placeholder="作者"></el-input>
          </el-form-item>
          <el-form-item label="版权协议" required>
            <el-select v-model="articleForm.copyright" placeholder="请选择版权协议">
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="立即发布" prop="isPublish">
            <el-switch v-model="articleForm.isPublish"></el-switch>
          </el-form-item>
          <el-form-item label="是否原创文章" prop="isOriginal">
            <el-radio-group v-model="articleForm.isOriginal">
              <el-radio :label="true">原创文章</el-radio>
              <el-radio :label="false">转载文章</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="一句话推荐" prop="recommendation">
            <el-input v-model="articleForm.recommendation"></el-input>
          </el-form-item>
          <el-form-item prop="copyrightIntro">
            <el-input type="textarea" v-model="articleForm.copyrightIntro"></el-input>
          </el-form-item>
          <el-form-item  style="height:360px;">
           <!-- bidirectional data binding（双向数据绑定） -->
            <quill-editor v-model="content"
              ref="myQuillEditor"
              :options="editorOption"
              @blur="onEditorBlur($event)"
              @focus="onEditorFocus($event)"
              @ready="onEditorReady($event)" style="height:280px;">
            </quill-editor>
          </el-form-item>
          <el-form-item label="关键词标签">
            <el-tag size="medium" :key="tag" v-for="tag in tags" closable :disable-transitions="false" @close="handleClose(tag)">{{tag}}</el-tag>
            <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" 
              @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
            </el-input>
            <el-button v-else class="button-new-tag" size="mini" @click="showInput">+ New Tag</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('articleForm')">创建文章</el-button>
            <el-button type="danger" @click="resetForm('articleForm')">清空</el-button>
          </el-form-item>
        </el-form>
      </div>

    </el-card>
  </div>
</template>
<style lang="scss" scoped>

  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }


  .quill-code {
    border: none;
    height: auto;
    > code {
      width: 100%;
      margin: 0;
      padding: 1rem;
      border: 1px solid #ccc;
      border-top: none;
      border-radius: 0;
      height: 10rem;
      overflow-y: auto;
      resize: vertical;
    }
  }
</style>
<script>
  export default {
    data() {
      return {
        tags: [],
        inputVisible: false,
        inputValue: '',
        content: '<h2>I am Example</h2>',
        editorOption: {
          // some quill options
        },
        articleForm: {
          title: '',
          description: '',
          author: '',
          copyright: '',
          copyrightList:[],
          isPublish: true,
          isOriginal: true,
          recommendation:'',
          reference: [],
          copyrightIntro:`如无特殊说明，本站内容（包含文字、图片、视频及其他形式的载体）均归属于miaoui.com，转载请联系 @妙UI 授权。原作品版权归属原作者。转载时请在文首注明，来源 miaoui.com ，欢迎关注 @妙ui `,
        },
        rules: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          date2: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
          ],
          type: [
            { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
          ],
          resource: [
            { required: true, message: '请选择活动资源', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '请填写活动形式', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {

      handleClose(tag) {
        this.tags.splice(this.tags.indexOf(tag), 1);
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.tags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },

      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },

       // manually control the data synchronization
      // 如果需要手动控制数据同步，父组件需要显式地处理changed事件
      onEditorBlur(quill) {
        console.log('editor blur!', quill)
      },
      onEditorFocus(quill) {
        console.log('editor focus!', quill)
      },
      onEditorReady(quill) {
        console.log('editor ready!', quill)
      },
      onEditorChange({ quill, html, text }) {
        console.log('editor change!', quill, html, text)
        this.content = html
      }

  },
  computed: {
      editor() {
        return this.$refs.myQuillEditor.quill
      }
    },
    mounted() {
      console.log('this is current quill instance object', this.editor)
    }
  }
</script>


